home *** CD-ROM | disk | FTP | other *** search
- ' Note the new keyword "App" that replaces "Lib <libname>" in the Declare
- ' statement when the function resides in the App that is calling Enable
-
- Declare Sub mcb App (ByVal ctx&, ByVal type%, ByVal errno%, ByVal str$)
- Declare Function OpenTextFile App (ByVal filename$) As Integer
- Declare Function CloseTextFile App (ByVal fid%) As Integer
- Declare Function ReadTextLine App (ByVal fid%, ByVal buf$) As Integer
- Declare Function MyGetOpenFilename App (ByVal filename$, ByVal fileExt$, ByVal fileInit$, ByVal filter$) As Integer
-
- Dim g_linebuf$ As String * 256
- Dim g_buflen% As Integer
- Dim g_filename$ As String * 256
-
- Sub Main ()
- Dim fid% As Integer
- Dim a_char$ As String
- Dim buf$ As String
- Dim appObj As object
- Dim addrBookObj As object
- Dim FileExt$ As String
- Dim FileInitName$ As String
- Dim FileFilter$ As String
-
- Set appObj = CreateObject("NetXRay.Application.1")
- Set addrBookObj = appObj.GetAddressBookDoc()
-
- FileInitName$ = "hosts"
- FileFilter$ = "All Files(*.*) | *.* ||"
- FileExt$ = " "
-
- bOpen% = MyGetOpenFilename(g_filename$, FileExt$, FileInitName$, FileFilter$)
- If bOpen% = 0 Then
- GoTo CancelOpen
- End If
-
- fid% = OpenTextFile(g_filename$)
- If fid% > 0 Then
- Do
- NextLine:
- g_buflen% = ReadTextLine(fid%, g_linebuf$)
- If g_buflen% <= 0 Then
- 'Exit Do
- GoTo EndOfFile
- End If
-
- buf$ = StripLeadingSpace(g_linebuf$)
-
- 'not enough data
- 'g_buflen% = Len(buf$)
- If Len(buf$) < 5 Then
- GoTo NextLine
- End If
-
- 'skip the comment
- a_char$ = Left$(buf$, 1)
- If a_char$ = "#" Then
- GoTo NextLine
- End If
-
- 'get the IP token
- ip$ = GetToken(buf$)
-
- bHasName = 0
- buf$ = Right$(buf$, Len(buf$)-Len(ip$))
- buf$ = StripLeadingSpace(buf$)
- If Len(buf$) > 0 Then
- 'get the 1st Name token
- name$ = GetToken(buf$)
- bHasName = 1
- End If
-
- bAddOK = 0
- If bHasName = 1 Then
- bAddOK = addrBookObj.AddNewAddr(name$, "000000000000", ip$, "", "From hosts file")
- End If
-
- Loop While g_buflen% > 0
-
- EndOfFile:
- CloseTextFile(fid%)
- End If
-
- Response = MsgBox("Import file completed!", MB_OK, "NetXRay")
-
- CancelOpen:
- i = 0
- End Sub
-
- 'Get a token
- Function GetToken (Buf$ As String) As String
- Dim a_char$, tabCh$, crCh$, lfCh$
- Dim temp$
- tabCh$ = Chr$(9)
- crCh$ = Chr$(13)
- lfCh$ = Chr$(10)
- Pos = 1
- buflen% = Len(Buf$)
- For Pos = 1 To buflen%
- a_char$ = Mid$(Buf$, Pos, 1)
- If a_char$ = " " Or a_char$ = tabCh$ Or a_char$ = crCh$ Or a_char$ = lfCh$ Then
- GetToken = temp$
- Exit Function
- End If
- If Pos = 1 Then
- temp$ = a_char$
- Else
- temp$ = temp$ + a_char$
- End If
- Next Pos
-
- GetToken = temp$
- End Function
-
- 'remove the leading space and tab chars
- Function StripLeadingSpace (Buf As String) As String
- Dim a_char$, tabCh$
- Dim temp$
- temp$ = Buf
- tabCh$ = Chr$(9)
- While (Len(temp$) > 0)
- a_char$ = Left$(temp$, 1)
- If a_char$ <> " " And a_char$ <> tabCh$ Then
- StripLeadingSpace = temp$
- Exit Function
- End If
- temp$ = Right$(temp$, Len(temp$) - 1)
- Wend
-
- StripLeadingSpace = temp$
- End Function
-